// pages/operation/signUp/signUpOption/index.js
import {
  SignUpModel
} from "../../../../api/api-work/signUpModel.js"
const signUpModel = new SignUpModel()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    activeTabs: "a",
    courseName: "",
    pageNum: 1,
    pageSize: 15,
    total: 0,
    tableDatas: [],
    selectList: [], //选中的列表
    istriggered: false,
    loadStatus: "",
    checked: [],
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {

  },
  /**
   * 生命周期函数--监听页面显示
   */
  async onShow() {
    this.setData({
      pageNum: 1,
      tableDatas: []
    })
    await this.getCourseList()
  },
  //获取课程列表
  async getCourseList() {
    signUpModel.getCourseListAPI({
      state: 0,
      pageNum: this.data.pageNum,
      pageSize: this.data.pageSize,
      name: this.data.courseName
    }).then(res => {
      let rows = this.data.tableDatas;
      rows.push(...res.rows);
      rows.forEach((row) => {
        row.tCoursePriceList.forEach((item) => {
          item.relName = `${item.name}(${item.totalPrice}元${
            item.num === 1 ? '/' : item.num
          }课时)`
        })
        row.courseName = row.name
        row.courseId = row.id
        row.priceId = row.tCoursePriceList[0].id
        row.relName=row.tCoursePriceList[0].relName
        row.courseAmount = row.tCoursePriceList[0].totalPrice
        row.discountPrice =
          row.tCoursePriceList[0].totalPrice -
          row.tCoursePriceList[0].discountedPrice
        if (row.tCoursePriceList[0].num > 0) {
          row.originalUnitPrice =
            row.tCoursePriceList[0].totalPrice / row.tCoursePriceList[0].num
        } else {
          row.originalUnitPrice = row.tCoursePriceList[0].totalPrice
        }
        row.countLessonTotalNormal = row.tCoursePriceList[0].num
        row.countLessonTotalGive = row.tCoursePriceList[0].freeClassHours ?
          row.tCoursePriceList[0].freeClassHours :
          0
        row.num = row.tCoursePriceList[0].num
        row.discountAmount = row.tCoursePriceList[0].discountedPrice
        // console.log('getLessonList', lessonList.value)
      })
      this.setData({
        tableDatas: rows,
        total: res.total
      })
      if (this.data.total == 0) {
        this.setData({
          // 无数据
          loadStatus: ""
        })
      } else if (this.data.tableDatas.length < this.data.total) {
        // 还有数据
        this.setData({
          // 加载更多
          loadStatus: "-"
        })
      } else {
        // 没有数据了
        this.setData({
          // 加载更多
          loadStatus: "nomore"
        })
      }
    })
  },
  //搜索课程
  searchCourse() {
    this.setData({
      pageNum: 1,
      tableDatas: []
    })
    this.getCourseList();
  },
  // 上拉加载
  getMoreDatas() {
    if (this.data.loadStatus === 'nomore') {
      // 没有更多了
      return;
    }
    this.setData({
      pageNum: this.data.pageNum + 1,
      loadStatus: "loading" // 加载中
    })
    this.getCourseList();
  },

  // 下拉刷新
  refreshMoreDatas() {
    this.setData({
      istriggered: false,
      pageNum: 1,
      tableDatas: []
    })
    this.getCourseList();
  },
  //选择课程
  onChooseCourse(event) {
    console.log('event', event);
    this.setData({
      checked: event.detail,
      selectList: []
    });
    this.data.checked.map(item => {
      this.data.selectList.push(this.data.tableDatas[item])
    })
    this.setData({
      selectList: this.data.selectList
    })
    console.log('selectList', this.data.selectList);
  },
  //确定按钮
  confirm() {
    var pages = getCurrentPages();
    var prevPage = pages[pages.length - 2]; // 上一个页面
    var payMoney = 0
    this.data.selectList.forEach((item) => {
      payMoney += parseInt(item.discountAmount)
    })
    const amount = payMoney - prevPage.data.outMoney;
    prevPage.setData({
      courseList: this.data.selectList,
      formattedAmount:amount % 1 === 0 ? Math.abs(amount) : Math.abs(amount).toFixed(2),
      payMoney
    });
    wx.navigateBack()
  },



  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})